package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.*;

import com.wzq.service.ITimeSelectionService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台管理-用户Controller控制器
 */
@RestController
@RequestMapping("/admin/timeselection")
public class AdminTimeselectionController extends GetSome {

    @Autowired
    private ITimeSelectionService iTimeselectionService;

    /**
     * 根据条件分页查询用户信息
     * @param pageBean
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<TimeSelection> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<TimeSelection> pageResult = iTimeselectionService.page(page, new QueryWrapper<TimeSelection>().in(StringUtil.isNotEmpty(query), "w_id", searchByWname(query)));
        List<TimeSelection> list=new ArrayList<>();
        for (TimeSelection timeselection : pageResult.getRecords()) {
            timeselection.setW_name( getW_name(timeselection.getW_id()));
            list.add(timeselection);
        }

        Map<String,Object> map=new HashMap<>();
        map.put("TimeselectionList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iTimeselectionService.removeById(id);
        return R.ok();
    }


    @RequestMapping("/save")        //添加修改
    public R save(@RequestBody TimeSelection timeselection){


        if(timeselection.getId()==null || timeselection.getId()==-1){
            iTimeselectionService.save(timeselection);
        }else{
            iTimeselectionService.saveOrUpdate(timeselection);
        }
        return R.ok();
    }

    @GetMapping("/{id}")            //单个回显
    public R findById(@PathVariable(value = "id")Integer id){
        TimeSelection timeselection = iTimeselectionService.getById(id);
        //        获得用户名称和项目名称
        Map<String,Object> map=new HashMap<>();
        map.put("timeselection",timeselection);
        return R.ok(map);
    }

    @RequestMapping("/listAll")
    public R listAll(){
        Map<String,Object> map=new HashMap<>();
        map.put("TimeSelection",iTimeselectionService.list());
        return R.ok(map);
    }


}
